<?php

$_autoload = array();
if(file_exists('_data/_data/autoload.ser')){
	$_autoload = unserialize(file_get_contents('_data/autoload.ser'));
}

function __autoload($class) {
    
	global $_autoload;
	
	if(array_key_exists($class, $_autoload)){
		include_once($_autoload[$class]);
	}
	//try find class in modluse
	$d = dir('modules/');

	if(file_exists("lib/" . $class . '.php')){
			
		include_once("lib/" . $class . '.php');
		$_autoload[$class] = "lib/" . $class . '.php';
		file_put_contents('_data/autoload.ser', serialize($_autoload));
		include_once("lib/" . $class . '.php');
		return ;
	}
	
	while (false !== ($entry = $d->read())) {
	   
		if($entry == '.' || $entry == '..'){
			continue;
		}
		
		if(file_exists($d->path . $entry . '/' . $class . '.php')){
			$_autoload[$class] = $d->path . $entry . '/' . $class;
			file_put_contents('_data/autoload.ser', serialize($_autoload));
			
			include_once($d->path . $entry . '/' . $class . '.php');
			break;
		}
		elseif (file_exists($d->path . $entry . '/' . $class . '.class.php')){
			$_autoload[$class] = $d->path . $entry . '/' . $class . '.class.php';
			file_put_contents('_data/autoload.ser', serialize($_autoload));
			include_once($d->path . $entry . '/' . $class . '.class.php');
			break;
		}
		elseif (file_exists($d->path . $entry . '/php/' . $class . '.php')){
		 	$_autoload[$class] = $d->path . $entry . '/php/' . $class . '.php';
			file_put_contents('_data/autoload.ser', serialize($_autoload));
			include_once($d->path . $entry . '/php/' . $class . '.php');
			break;
		}
		elseif (file_exists($d->path . $entry . '/php/' . $class . '.class.php')){
			$_autoload[$class] = $d->path . $entry . '/php/' . $class . '.class.php';
			file_put_contents('_data/autoload.ser', serialize($_autoload));
			include_once($d->path . $entry . '/php/' . $class . '.class.php');
			break;
		}
	}
	$d->close();
	
}